package com.atguigu.cloud.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.cloud.dto.PayDto;
import com.atguigu.cloud.resp.ResultData;
import com.atguigu.cloud.resp.ReturnCodeEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
public class PayAlibabaController {

    @Value("${server.port}")
    private String port;

    @GetMapping(value = "/pay/nacos/{id}")
    public String getPayInfo(@PathVariable("id") Integer id) {
        return "nacos registry, server port is " + port + ", \t id is " + id;
    }

    /**
     * openFeign + sentinel 进行服务降级和流量监控的整合处理
     */
    @GetMapping(value = "/pay/nacos/get/{orderNo}")
    @SentinelResource(value = "getPayOrderNo", blockHandler = "handlerBlockHandler")
    public ResultData getPayByOrderNo(@PathVariable("orderNo") String orderNo) {
        PayDto payDto = new PayDto();
        payDto.setId(1024);
        payDto.setOrderNo(orderNo);
        payDto.setAmount(BigDecimal.valueOf(9.9));
        payDto.setPayNo("pay: " + IdUtil.simpleUUID());
        payDto.setUserId(1);
        return ResultData.success("查询返回值: " + payDto);
    }

    public ResultData handlerBlockHandler(@PathVariable("orderNo") String orderNo, BlockException e) {
        return ResultData.fail(ReturnCodeEnum.RC500.getCode(), "getPayByOrderNo接口不可用" + "sentinel流控配置规则");
    }

}
